/*
 * QuestInfo.h
 *
 *  Created on: 2013. 8. 28.
 *      Author: jjaehuny
 */

#ifndef PROJECTZ_NLOGIC_QUESTINFO_H_
#define PROJECTZ_NLOGIC_QUESTINFO_H_

#include "../NUtil/Pool.h"
#include <inttypes.h>

#include <map>
#include <vector>

namespace ProjectZ {
class User;
} /*ProjectZ*/


namespace ProjectZ { namespace NLogic {

class QuestInfo
{
public:
	class Quest
	{
	public:
		struct Data
		{
			uint16_t	_questID;	// QUEST_ID
			uint8_t 	_idx;		// active quest list index

			/* state
				QS_NONE,			/// -1: 퀘스트 미부여/미진행
				QS_STARTABLE,		/// 0 : 퀘스트 시작 가능
				QS_PROGRESS,		/// 1 : 퀘스트 진행 중
				QS_SUCCESS,		/// 2 : 퀘스트 완료 가능
				QS_COMPLETE,		/// 3 : 퀘스트 완결
				QS_FAILED,			/// 4 : 퀘스트 실패
			*/
			int8_t 	_state;	// state

			uint8_t 	_achieve;	// count data

		public:
			Data() : _questID(0), _idx(0), _state(0), _achieve(0) { }

		public:
			void Clear() {
				_questID = 0;
				_idx = 0;
				_state = 0;
				_achieve = 0;
			}
		};

	public:
		Quest();
		virtual ~Quest();

	public:
		void 			Initialize() { _data.Clear(); _user = NULL; }
		void 			Finalize() {}

	public:
		void 			Open (Data & data, User * pUser);
		Data &			GetData();

		bool			InsertQuestInfo();
		bool			UpdateQuestData();
		bool			UpdateQuestState();

		//bool			DeleteQuestInfo();

	private:
		Data 			_data;
		User *			_user;
	};

public:
	QuestInfo(User * pUser);
	~QuestInfo();

	void			Initialize();
	void			Finalize();

	bool 			QuestHistoryLoadFromDatabase();
	bool			QuestLoadFromDatabase();

public:
	typedef std::map<uint16_t, NLogic::QuestInfo::Quest *>		QUESTTREE;
	typedef std::vector<uint16_t> 						QUESTHISTORYVECTOR;

public:

	int 					GetQuestCount() { return clsQuestTree.size(); }
	bool 					AddQuest(Quest * pQuest);
	bool					RemoveQuest(uint16_t questID);
	bool					RemoveQuestOnlyTree(uint16_t questID);
	Quest *				GetQuest(uint16_t questID);
	QUESTTREE &			GetQuestTree() { return clsQuestTree; }

	int 					GetQuestHistoryCount() { return clsQuestHistory.size(); }
	bool					AddQuestHistory(uint16_t questID);
	bool					IsExistQuestHistory(uint16_t questID);
	QUESTHISTORYVECTOR &	GetQuestHistoryVector() { return clsQuestHistory; }

private:
	void					addQuestFromDatabase(Quest * pQuest);

private:
	QUESTTREE				clsQuestTree;
	QUESTHISTORYVECTOR	clsQuestHistory;

	User * 				_user;
};

} /*NLogic*/ } /* namespace ProjectZ */

#endif /* PROJECTZ_NLOGIC_QUESTINFO_H_ */
